package com.ruipengkj.component.jdialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.RPJLabelTitle;
import com.ruipengkj.factory.ImageFactory;

public class TitlePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private TexturePaint paint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.DIALOG_TITLE_BG).getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.DIALOG_TITLE_BG_LEFT).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.DIALOG_TITLE_BG_RIGHT).getImage();
	
	private Icon closeIcon = ImageFactory.loadImage(ImageFactory.TabbedPane_close);
	private JButton closeButton = new JButton("", closeIcon);
	
	private RPJLabelTitle title;
	public TitlePanel() {
		this.setLayout(new BorderLayout());
		Util.setSize(this, 35, 35);
		init();
	}
	
	private void init() {
		Util.setSize(closeButton, 35, 35);
		closeButton.setFocusable(false);
		closeButton.setBorder(null);
		closeButton.setContentAreaFilled(false);
		closeButton.setCursor(Util.CURSOR_HAND);
		this.add(closeButton, BorderLayout.EAST);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setPaint(paint);
		g2d.fillRect(5, 0, this.getWidth()-6, this.getHeight());
		g2d.drawImage(backgroundLeftImage, 0, 0, null);
		int x = this.getWidth() - backgroundRightImage.getWidth(null);
		int y = 0;
		g2d.drawImage(backgroundRightImage, x, y, null);
	}
	
	public JButton getCloseButton() {
		return closeButton;
	}
	
	public void setCloseButton(JButton closeButton) {
		this.closeButton = closeButton;
	}
	
	public void setTitle(String title, int x, int y) {
		this.title = new RPJLabelTitle(title);
		Util.setSize(this.title, x, y);
		JPanel jp = new JPanelFlowLayoutComponent(this.title, FlowLayout.RIGHT);
		Util.setSize(jp, x+16, 35);
		this.add(jp, BorderLayout.WEST);
	}
}
